﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Input;
using CommunityToolkit;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HeimaSoft.Model;
using HeimaSoft.View;
using PetaPoco;

namespace HeimaSoft.ViewModel
{
    public partial class {tblName}ListViewModel : BaseViewModel
    {
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public {tblName}ListViewModel()
        {
            InitPage();
            Pager = new Cls_Pager();
            LoadDataCommand = new RelayCommand(LoadData);
            LoadData();

        }
        #endregion

        #region 业务函数
        /// <summary>
        /// 初始化查询条件
        /// </summary>
        private void InitPage()
        {
            //PostData.Start_date= DateTime.Now;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        private void LoadData()
        {

            var sql = new Sql("select * from {tblName}");
  [loop start]
            if (HasValue(PostData.{fldName}))
            {
                sql.Where("[fldName]=@0", PostData.{fldName}); //[fldTitle]
            }

  [loop end]^
            sql.OrderBy(" id desc");

            var data = db.Page<{tblName}>(Pager.Page_index, Pager.Page_size, sql);
            QueryResult.Items = data.Items;
            Pager.Page_count = (int)data.TotalPages;
            Pager.PreviousEnabled = Pager.Page_index > 1;
            Pager.NextEnabled=Pager.Page_index < data.TotalPages;

        }
        #endregion

        #region 实体对象
        /// <summary>
        /// 查询条件
        /// </summary>
        [ObservableProperty]
        Cls_post_data postData = new Cls_post_data();

        [ObservableObject]
        public partial class Cls_post_data
        {
            //[ObservableProperty]
            //DateTime? start_date; //开始日期
[loop start]
			[ObservableProperty]
			public [fldType] [fldName]; //[fldTitle]
[loop end]^

        }

        /// <summary>
        /// 查询结果
        /// </summary>
        [ObservableProperty]
        Cls_query_result queryResult = new Cls_query_result();

        [ObservableObject]
        public partial class Cls_query_result
        {
            [ObservableProperty]
            public List<{tblName}> items  = new List<{tblName}>();
        }
        [ObservableProperty]
        private Cls_Pager pager;

        // 分页查询命令
        public ICommand LoadDataCommand { get; }
        #endregion

        #region 辅助函数
        [RelayCommand]
        public void BtnOpenAdd()
        {
            SetValue("id", 0);
            OpenLayer(new {tblName}EditPage(), "新增");
            LoadData();

        }
        [RelayCommand]
        public void BtnOpenEdit(object parameter)
        {
            if (parameter is {tblName} mod)
            {
                SetValue("id", mod.id);
                OpenLayer(new {tblName}EditPage(), "修改");
                LoadData();

            }
        }
        [RelayCommand]
        public void BtnDel(object parameter)
        {

            if (LayerConfirm("是否确定删除？删除后不能恢复。") && parameter is {tblName} mod)
            {
                db.Delete(mod);
                LoadData();
            }
        }
        [RelayCommand]
        public void BtnQuery()
        {
            Pager.Page_index = 1;
            LoadData();
        }
        [RelayCommand]
        public void RefreshPage()
        {
            LoadData();
        }
        #endregion


    }
}




